<?php
/**
 * sitedefine.php
 * Create on 2015-03-25 10:53:18
 * Create by yuebin
 */
namespace haibao\cms\data\site;

use haibao\frame\data\query\Condition;

class Site extends \haibao\cms\data\BaseMysql{

	private static $xcacheSiteTreeName = 'SITE_TREE';
	
    public function __construct(){
        parent::__construct('\haibao\cms\model\data\site\SiteDefine');
    }

 	/**
 	 * 添加站点
	 * @param : Object \haibao\cms\model\data\site\SiteDefine $siteDefineModel
 	 */
    public function addSiteDefine($siteDefineModel){
    	$this->beginTranscation();
    	try{
    		$this->add($siteDefineModel);
    		$this->clearCache();
    	} catch(\Exception $e){
    		$this->rollback();
    		throw $e;
    	}
    	$this->endTranscation();
    }
    
    /**
     * 根据站点ID更新站点
	 * @param : Object \haibao\cms\model\data\site\SiteDefine $siteDefineModel
     */
    public function updateSiteDefineById($siteDefineModel){
    	$arrSet = 
    		array(
    			\haibao\cms\model\filter\site\SiteDefine::CONDITION_FIELD_NAME_SITE_NAME=>$siteDefineModel->SiteName,			
    			\haibao\cms\model\filter\site\SiteDefine::CONDITION_FIELD_NAME_SITE_URL=>$siteDefineModel->SiteUrl,			
    		);
    	$this->beginTranscation();
    	try{
    		$this->updateById($siteDefineModel->Id, $arrSet);
    		$this->clearCache();
    	} catch(\Exception $e){
    		$this->rollback();
    		throw $e;
    	}
    	$this->endTranscation();
    }
    
    /**
     * 根据站点ID获取站点信息
     * @param : Int $siteId
     */
    public function getSiteDefineById($siteId){
    	$model = null;
    	$cacheSiteTree = $this->getCache(self::$xcacheSiteTreeName);
    	if($cacheSiteTree){
    		if(isset($cacheSiteTree[$siteId])){
    			$model = new \haibao\cms\model\data\site\SiteDefine();
    			$model->loadArray($cacheSiteTree[$siteId]);
    			$model->Id = $siteId;
    		}
    	}
    	if(empty($model)){
    		
    		$model = $this->getOneById($siteId);
    	}
    	return $model;
    }
    /**
     * 根据filter获取站点信息
     * @param : Object \haibao\cms\model\filter\site\SiteDefine $filter
     */
    public function getSiteDefineByFilter($filter){
    	return $this->getOne($filter);
    }
    
    /**
     * 判断当前站点下是否存在频道
     * @pamam : Int $siteId
     */
    public function isExistChannel($siteId){
    	$sql = "select count(*) as channelNum from `SiteChannel` where SiteId=%s and status=%s";
    	$siteInfo = $this->query($sql,array($siteId,\haibao\cms\model\data\site\SiteChannel::STATUS_ENABLE))->fetch_assoc();
    	return !empty($siteInfo['channelNum']) ? true : false;
    }
    
    /**
     * 删除站点
     * @param : Int $siteId
     */
    public function deleteSiteDefineById($siteId){
    	$this->updateById($siteId,
    		array(
    			\haibao\cms\model\filter\site\SiteDefine::CONDITION_FIELD_NAME_STATUS => \haibao\cms\model\data\site\SiteDefine::STATUS_DIS_ENABLE
    		)
    	);
    	$this->clearCache();
    }
    
    /**
     * 根据站点名字获取站点信息
     * @param : String $siteName
     * @param : Int $siteId
     */
    public function getSiteBySiteName($siteName, $siteId = null){
    	$model = null;
    	$sql = "select * from `SiteDefine` where SiteName=%s and Status=%s";
    	$valArr = array($siteName,\haibao\cms\model\data\site\SiteDefine::STATUS_ENABLE);
    	if($siteId){
    		$sql .= " and Id <>%s";
    		array_push($valArr,$siteId);
    	}
    	$result = $this->query($sql, $valArr);
    	while ($result->fetch_assoc()){
    		$model = new \haibao\cms\model\data\site\SiteDefine();
    		foreach($model->getKeyMap() as $keyName=>$keyType){
    			$model->$keyName = $result->getData($keyType->getFieldName());
    		}
    	}
    	return $model;
    }
    
    /**
     * 获取站点树
     */
    public function getSiteDefineTree($siteId){
    	// TODO : 从cache 中获取数据（暂时不处理）
    	$cacheSiteTree = $this->getCache(self::$xcacheSiteTreeName);
    	$siteDefineTree = $cacheSiteTree ? $cacheSiteTree : $this->setSiteDefineTree();
    	//$siteDefineTree = $this->setSiteDefineTree();
    	if($siteId){
    		return $siteDefineTree[intval($siteId)];
    	}
    	return $siteDefineTree;
    }
    
    private function setSiteDefineTree(){
    	$siteDefineArr = array();
    	$siteDefineFilter = new \haibao\cms\model\filter\site\SiteDefine();
    	$siteDefineFilter->select(
    		array(
    			\haibao\cms\model\filter\site\SiteDefine::CONDITION_FIELD_NAME_ID,
    			\haibao\cms\model\filter\site\SiteDefine::CONDITION_FIELD_NAME_SITE_NAME,
    			\haibao\cms\model\filter\site\SiteDefine::CONDITION_FIELD_NAME_SITE_URL,
    			
    		)
    	);
		$siteDefineFilter->where(\haibao\cms\model\filter\site\SiteDefine::CONDITION_FIELD_NAME_STATUS,Condition::CONDITION_EQUAL,\haibao\cms\model\data\site\SiteDefine::STATUS_ENABLE);
    	$siteChannelFilter = new \haibao\cms\model\filter\site\SiteChannel();
    	$siteChannelFilter->select(
    		array(
    			\haibao\cms\model\filter\site\SiteChannel::CONDITION_FIELD_NAME_ID,
    			\haibao\cms\model\filter\site\SiteChannel::CONDITION_FIELD_NAME_SITE_ID,
    			\haibao\cms\model\filter\site\SiteChannel::CONDITION_FIELD_NAME_CHANNEL_NAME,
    			\haibao\cms\model\filter\site\SiteChannel::CONDITION_FIELD_NAME_CHANNEL_URL,
    			\haibao\cms\model\filter\site\SiteChannel::CONDITION_FIELD_NAME_PARENT_ID,
    			\haibao\cms\model\filter\site\SiteChannel::CONDITION_FIELD_NAME_STATUS,
    			\haibao\cms\model\filter\site\SiteChannel::CONDITION_FIELD_NAME_ORDER_NO,
    		)
    	);
    	//$siteChannelFilter->where(\haibao\cms\model\filter\site\SiteChannel::CONDITION_FIELD_NAME_STATUS,Condition::CONDITION_EQUAL,\haibao\cms\model\data\site\SiteChannel::STATUS_ENABLE);
    	$siteDefineFilter->order(\haibao\cms\model\filter\site\SiteChannel::CONDITION_FIELD_NAME_ORDER_NO,true,$siteChannelFilter);
    	$siteDefineFilter->leftJoin($siteChannelFilter,null,\haibao\cms\model\filter\site\SiteDefine::CONDITION_FIELD_NAME_ID,\haibao\cms\model\filter\site\SiteChannel::CONDITION_FIELD_NAME_SITE_ID,\haibao\cms\model\filter\site\SiteDefine::CONDITION_FIELD_NAME_SITE_CHANNEL);
   
    	$siteDefines = $this->getAll($siteDefineFilter);
    	
    	foreach($siteDefines as $siteDefine){
    		
    		$tempArr = $siteChannelArr = array();
    		$tempArr['SiteName'] = $siteDefine->SiteName;
    		$tempArr['SiteUrl'] = $siteDefine->SiteUrl;
    		$tempArr['SiteChannel'] = array();
    		if($siteDefine->SiteChannel){
    			foreach($siteDefine->SiteChannel as $channel){
    				$siteChannelArr[$channel->Id] = $channel->toArray();
    			}
    			$temp = array();
    			foreach($siteChannelArr as $val){	
    				if(isset($siteChannelArr[$val[\haibao\cms\model\filter\site\SiteChannel::CONDITION_FIELD_NAME_PARENT_ID]])){
    					$siteChannelArr[$val[\haibao\cms\model\filter\site\SiteChannel::CONDITION_FIELD_NAME_PARENT_ID]]['children'][] = &$siteChannelArr[$val[\haibao\cms\model\filter\site\SiteChannel::CONDITION_FIELD_NAME_ID]];
    				}else{
    					//$temp[$val[\haibao\cms\model\filter\site\SiteChannel::CONDITION_FIELD_NAME_ID]] = &$siteChannelArr[$val[\haibao\cms\model\filter\site\SiteChannel::CONDITION_FIELD_NAME_ID]];
    					/*json_encode 对key进行排序*/
    					$temp[] = &$siteChannelArr[$val[\haibao\cms\model\filter\site\SiteChannel::CONDITION_FIELD_NAME_ID]];
    				}
    			}
    			$tempArr['SiteChannel'] = $temp;
    			uasort($tempArr['SiteChannel'], 'self::sortSiteChannel');
    			
    			 
    		}
    		$siteDefineArr[$siteDefine->Id] = $tempArr;
    	}
    	$this->setCache(self::$xcacheSiteTreeName, $siteDefineArr);
    	return $siteDefineArr;
    }
    
    public function clearCache(){
    	
    	$this->unsetCache(self::$xcacheSiteTreeName);
    }
    
    public static function sortSiteChannel($orderAsc,$orderDesc){
    	return $orderAsc['OrderNo'] - $orderDesc['OrderNo'];
    }
}
